home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gd25s.zip / CO.C < prev    next >
C/C++ Source or Header  |  1993-11-11  |  22KB  |  791 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991, 1992 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkout operation
  29.  */
  30. /*****************************************************************************
  31.  *                       check out revisions from RCS files
  32.  *****************************************************************************
  33.  */
  34.  
  35.  
  36. /* $Log: co.c,v $
  37.  * Revision 5.13  1992/07/28  16:12:44  eggert
  38.  * Add -V.  Check that working and RCS files are distinct.
  39.  *
  40.  * Revision 5.12  1992/02/17  23:02:08  eggert
  41.  * Add -T.
  42.  *
  43.  * Revision 5.11  1992/01/24  18:44:19  eggert
  44.  * Add support for bad_creat0.  lint -> RCS_lint
  45.  *
  46.  * Revision 5.10  1992/01/06  02:42:34  eggert
  47.  * Update usage string.
  48.  *
  49.  * Revision 5.9  1991/10/07  17:32:46  eggert
  50.  * -k affects just working file, not RCS file.
  51.  *
  52.  * Revision 5.8  1991/08/19  03:13:55  eggert
  53.  * Warn before removing somebody else's file.
  54.  * Add -M.  Fix co -j bugs.  Tune.
  55.  *
  56.  * Revision 5.7  1991/04/21  11:58:15  eggert
  57.  * Ensure that working file is newer than RCS file after co -[lu].
  58.  * Add -x, RCSINIT, MS-DOS support.
  59.  *
  60.  * Revision 5.6  1990/12/04  05:18:38  eggert
  61.  * Don't checkaccesslist() unless necessary.
  62.  * Use -I for prompts and -q for diagnostics.
  63.  *
  64.  * Revision 5.5  1990/11/01  05:03:26  eggert
  65.  * Fix -j.  Add -I.
  66.  *
  67.  * Revision 5.4  1990/10/04  06:30:11  eggert
  68.  * Accumulate exit status across files.
  69.  *
  70.  * Revision 5.3  1990/09/11  02:41:09  eggert
  71.  * co -kv yields a readonly working file.
  72.  *
  73.  * Revision 5.2  1990/09/04  08:02:13  eggert
  74.  * Standardize yes-or-no procedure.
  75.  *
  76.  * Revision 5.0  1990/08/22  08:10:02  eggert
  77.  * Permit multiple locks by same user.  Add setuid support.
  78.  * Remove compile-time limits; use malloc instead.
  79.  * Permit dates past 1999/12/31.  Switch to GMT.
  80.  * Make lock and temp files faster and safer.
  81.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  82.  *
  83.  * Revision 4.7  89/05/01  15:11:41  narten
  84.  * changed copyright header to reflect current distribution rules
  85.  * 
  86.  * Revision 4.6  88/08/09  19:12:15  eggert
  87.  * Fix "co -d" core dump; rawdate wasn't always initialized.
  88.  * Use execv(), not system(); fix putchar('\0') and diagnose() botches; remove lint
  89.  * 
  90.  * Revision 4.5  87/12/18  11:35:40  narten
  91.  * lint cleanups (from Guy Harris)
  92.  * 
  93.  * Revision 4.4  87/10/18  10:20:53  narten
  94.  * Updating version numbers changes relative to 1.1, are actually
  95.  * relative to 4.2
  96.  * 
  97.  * Revision 1.3  87/09/24  13:58:30  narten
  98.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  99.  * warnings)
  100.  * 
  101.  * Revision 1.2  87/03/27  14:21:38  jenkins
  102.  * Port to suns
  103.  * 
  104.  * Revision 4.2  83/12/05  13:39:48  wft
  105.  * made rewriteflag external.
  106.  * 
  107.  * Revision 4.1  83/05/10  16:52:55  wft
  108.  * Added option -u and -f.
  109.  * Added handling of default branch.
  110.  * Replaced getpwuid() with getcaller().
  111.  * Removed calls to stat(); now done by pairfilenames().
  112.  * Changed and renamed rmoldfile() to rmworkfile().
  113.  * Replaced catchints() calls with restoreints(), unlink()--link() with rename();
  114.  * 
  115.  * Revision 3.7  83/02/15  15:27:07  wft
  116.  * Added call to fastcopy() to copy remainder of RCS file.
  117.  *
  118.  * Revision 3.6  83/01/15  14:37:50  wft
  119.  * Added ignoring of interrupts while RCS file is renamed; this avoids
  120.  * deletion of RCS files during the unlink/link window.
  121.  *
  122.  * Revision 3.5  82/12/08  21:40:11  wft
  123.  * changed processing of -d to use DATEFORM; removed actual from
  124.  * call to preparejoin; re-fixed printing of done at the end.
  125.  *
  126.  * Revision 3.4  82/12/04  18:40:00  wft
  127.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE.
  128.  * Fixed printing of "done".
  129.  *
  130.  * Revision 3.3  82/11/28  22:23:11  wft
  131.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  132.  * %02d with %.2d, mode generation for working file with WORKMODE.
  133.  * Fixed nil printing. Fixed -j combined with -l and -p, and exit
  134.  * for non-existing revisions in preparejoin().
  135.  *
  136.  * Revision 3.2  82/10/18  20:47:21  wft
  137.  * Mode of working file is now maintained even for co -l, but write permission
  138.  * is removed.
  139.  * The working file inherits its mode from the RCS file, plus write permission
  140.  * for the owner. The write permission is not given if locking is strict and
  141.  * co does not lock.
  142.  * An existing working file without write permission is deleted automatically.
  143.  * Otherwise, co asks (empty answer: abort co).
  144.  * Call to getfullRCSname() added, check for write error added, call
  145.  * for getlogin() fixed.
  146.  *
  147.  * Revision 3.1  82/10/13  16:01:30  wft
  148.  * fixed type of variables receiving from getc() (char -> int).
  149.  * removed unused variables.
  150.  */
  151.  
  152.  
  153.  
  154.  
  155. #include "rcsbase.h"
  156.  
  157. static char const *getancestor P((char const*,char const*));
  158. static int buildjoin P((char const*));
  159. static int preparejoin P((void));
  160. static int rmlock P((struct hshentry const*));
  161. static int rmworkfile P((void));
  162. static void cleanup P((void));
  163.  
  164. static char const quietarg[] = "-q";
  165.  
  166. static char const *expandarg, *join, *suffixarg, *versionarg;
  167. static char const *joinlist[joinlength]; /* revisions to be joined */
  168. static FILE *neworkptr;
  169. static int exitstatus;
  170. static int forceflag;
  171. static int lastjoin;            /* index of last element in joinlist  */
  172. static int lockflag; /* -1 -> unlock, 0 -> do nothing, 1 -> lock */
  173. static int mtimeflag;
  174. static struct hshentries *gendeltas;    /* deltas to be generated    */
  175. static struct hshentry *targetdelta;    /* final delta to be generated    */
  176. static struct stat workstat;
  177.  
  178. mainProg(coId, "co", "$Id: co.c,v 5.13 1992/07/28 16:12:44 eggert Exp $")
  179. {
  180.     static char const cmdusage[] =
  181.         "\nco usage: co -{fIlMpqru}[rev] -ddate -jjoins -ksubst -sstate -w[who] -Vn -xsuff file ...";
  182.  
  183.     char *a, **newargv;
  184.     char const *author, *date, *rev, *state;
  185.     char const *joinname, *newdate, *neworkname;
  186.     int changelock;  /* 1 if a lock has been changed, -1 if error */
  187.     int argc_min, expmode, r, tostdout, workstatstat;
  188.     int Ttimeflag;
  189.     struct buf numericrev;    /* expanded revision number    */
  190.     char finaldate[datesize];
  191.  
  192.     setrid();
  193.     argc_min = 1;
  194.     author = date = rev = state = nil;
  195.     bufautobegin(&numericrev);
  196.     expmode = -1;
  197.     suffixes = X_DEFAULT;
  198.     tostdout = false;
  199.     Ttimeflag = false;
  200.  
  201.     argc = getRCSINIT(argc, argv, &newargv);
  202.     argv = newargv;
  203.     while (a = *++argv,  0<--argc && *a++=='-') {
  204.         switch (*a++) {
  205.  
  206.                 case 'r':
  207.         revno:
  208.             if (*a) {
  209.                 if (rev) warn("redefinition of revision number");
  210.                 rev = a;
  211.                         }
  212.                         break;
  213.  
  214.         case 'f':
  215.             forceflag=true;
  216.             goto revno;
  217.  
  218.                 case 'l':
  219.             if (lockflag < 0) {
  220.                                 warn("-l overrides -u.");
  221.                         }
  222.             lockflag = 1;
  223.                         goto revno;
  224.  
  225.                 case 'u':
  226.             if (0 < lockflag) {
  227.                                 warn("-l overrides -u.");
  228.                         }
  229.             lockflag = -1;
  230.                         goto revno;
  231.  
  232.                 case 'p':
  233.             tostdout = true;
  234. #if !text_work_stdio && !text_equals_binary_stdio
  235.             setmode(1, O_BINARY);
  236. #endif
  237.                         goto revno;
  238.  
  239.         case 'I':
  240.             interactiveflag = true;
  241.             goto revno;
  242.  
  243.                 case 'q':
  244.                         quietflag=true;
  245.                         goto revno;
  246.  
  247.                 case 'd':
  248.             if (date)
  249.                 redefined('d');
  250.             str2date(a, finaldate);
  251.                         date=finaldate;
  252.                         break;
  253.  
  254.                 case 'j':
  255.             if (*a) {
  256.                 if (join) redefined('j');
  257.                 join = a;
  258.                         }
  259.                         break;
  260.  
  261.         case 'M':
  262.             mtimeflag = true;
  263.             goto revno;
  264.  
  265.                 case 's':
  266.             if (*a) {
  267.                 if (state) redefined('s');
  268.                 state = a;
  269.                         }
  270.                         break;
  271.  
  272.         case 'T':
  273.             if (*a)
  274.                 goto unknown;
  275.             Ttimeflag = true;
  276.             break;
  277.  
  278.                 case 'w':
  279.             if (author) redefined('w');
  280.             if (*a)
  281.                 author = a;
  282.             else
  283.                 author = getcaller();
  284.                         break;
  285.  
  286.         case 'x':
  287.             suffixarg = *argv;
  288.             suffixes = a;
  289.             break;
  290.  
  291.         case 'V':
  292.             versionarg = *argv;
  293.             argc_min = setRCSversion(versionarg);
  294.             break;
  295.  
  296.         case 'k':    /*  set keyword expand mode  */
  297.             expandarg = *argv;
  298.             if (0 <= expmode) redefined('k');
  299.             if (0 <= (expmode = str2expmode(a)))
  300.                 break;
  301.             /* fall into */
  302.                 default:
  303.         unknown:
  304.             faterror("unknown option: %s%s", *argv, cmdusage);
  305.  
  306.                 };
  307.         } /* end of option processing */
  308.  
  309.     if (argc < argc_min) faterror("no input file%s", cmdusage);
  310.     if (tostdout)
  311. #        if text_equals_binary_stdio || text_work_stdio
  312.         workstdout = stdout;
  313. #        else
  314.         if (!(workstdout = fdopen(STDOUT_FILENO, FOPEN_W_WORK)))
  315.             efaterror("stdout");
  316. #        endif
  317.  
  318.     /* Now handle all pathnames.  */
  319.     for (;  0 < argc;  cleanup(), ++argv, --argc) {
  320.     ffree();
  321.  
  322.     if (pairnames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, false)  <=  0)
  323.         continue;
  324.  
  325.     /*
  326.      * RCSname contains the name of the RCS file, and finptr
  327.      * points at it.  workname contains the name of the working file.
  328.      * Also, RCSstat has been set.
  329.          */
  330.     diagnose("%s  -->  %s\n", RCSname, tostdout?"stdout":workname);
  331.  
  332.     workstatstat = -1;
  333.     if (tostdout) {
  334.         neworkname = 0;
  335.         neworkptr = workstdout;
  336.     } else {
  337.         workstatstat = stat(workname, &workstat);
  338.         if (workstatstat == 0  &&  same_file(RCSstat, workstat, 0)) {
  339.             error("RCS and working files are the same");
  340.             continue;
  341.         }
  342.         neworkname = makedirtemp(1);
  343.         if (!(neworkptr = fopen(neworkname, FOPEN_W_WORK))) {
  344.             if (errno == EACCES)
  345.                 error("%s: parent directory isn't writable",
  346.                     workname
  347.                 );
  348.             else
  349.                 eerror(neworkname);
  350.             continue;
  351.         }
  352.     }
  353.  
  354.         gettree();  /* reads in the delta tree */
  355.  
  356.         if (Head==nil) {
  357.                 /* no revisions; create empty file */
  358.         diagnose("no revisions present; generating empty revision 0.0\n");
  359.         Ozclose(&fcopy);
  360.         if (workstatstat == 0)
  361.             if (!rmworkfile()) continue;
  362.         changelock = 0;
  363.         newdate = 0;
  364.                 /* Can't reserve a delta, so don't call addlock */
  365.         } else {
  366.                 if (rev!=nil) {
  367.                         /* expand symbolic revision number */
  368.             if (!expandsym(rev, &numericrev))
  369.                                 continue;
  370.         } else
  371.             switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
  372.                 default:
  373.                 continue;
  374.                 case 0:
  375.                 bufscpy(&numericrev, Dbranch?Dbranch:"");
  376.                 break;
  377.                 case 1:
  378.                 bufscpy(&numericrev, targetdelta->num);
  379.                 break;
  380.             }
  381.                 /* get numbers of deltas to be generated */
  382.         if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
  383.                         continue;
  384.                 /* check reservations */
  385.         changelock =
  386.             lockflag < 0 ?
  387.                 rmlock(targetdelta)
  388.             : lockflag == 0 ?
  389.                 0
  390.             :
  391.                 addlock(targetdelta, true);
  392.  
  393.         if (
  394.             changelock < 0 ||
  395.             changelock && !checkaccesslist() ||
  396.             dorewrite(lockflag, changelock) != 0
  397.         )
  398.             continue;
  399.  
  400.         if (0 <= expmode)
  401.             Expand = expmode;
  402.         if (0 < lockflag  &&  Expand == VAL_EXPAND) {
  403.             error("cannot combine -kv and -l");
  404.             continue;
  405.         }
  406.  
  407.                 if (join && !preparejoin()) continue;
  408.  
  409.         diagnose("revision %s%s\n",targetdelta->num,
  410.              0<lockflag ? " (locked)" :
  411.              lockflag<0 ? " (unlocked)" : "");
  412.  
  413.         /* Prepare to remove old working file if necessary.  */
  414.         if (workstatstat == 0)
  415.                         if (!rmworkfile()) continue;
  416.  
  417.                 /* skip description */
  418.                 getdesc(false); /* don't echo*/
  419.  
  420.         locker_expansion = 0 < lockflag;
  421.         joinname = buildrevision(
  422.             gendeltas, targetdelta,
  423.             join&&tostdout ? (FILE*)0 : neworkptr,
  424.             Expand!=OLD_EXPAND
  425.         );
  426. #        if !large_memory
  427.             if (fcopy == neworkptr)
  428.                 fcopy = 0;  /* Don't close it twice.  */
  429. #        endif
  430.         if_advise_access(changelock && gendeltas->first!=targetdelta,
  431.             finptr, MADV_SEQUENTIAL
  432.         );
  433.  
  434.         if (donerewrite(changelock, Ttimeflag) != 0)
  435.             continue;
  436.  
  437.         newdate = targetdelta->date;
  438.         if (join) {
  439.             newdate = 0;
  440.             if (!joinname) {
  441.                 aflush(neworkptr);
  442.                 joinname = neworkname;
  443.             }
  444.             if (!buildjoin(joinname))
  445.                 continue;
  446.         }
  447.         }
  448.     if (!tostdout) {
  449.         mode_t m = WORKMODE(RCSstat.st_mode,
  450.         !(Expand==VAL_EXPAND || lockflag<=0&&StrictLocks)
  451.         );
  452.         time_t t = mtimeflag&&newdate ? date2time(newdate) : (time_t)-1;
  453.         ignoreints();
  454.         r = chnamemod(&neworkptr, neworkname, workname, 1, m, t);
  455.         keepdirtemp(neworkname);
  456.         restoreints();
  457.         if (r != 0) {
  458.         eerror(workname);
  459.         error("see %s", neworkname);
  460.         continue;
  461.         }
  462.         diagnose("done\n");
  463.     }
  464.     }
  465.  
  466.     tempunlink();
  467.     Ofclose(workstdout);
  468.     exitmain(exitstatus);
  469.  
  470. }       /* end of main (co) */
  471.  
  472.     static void
  473. cleanup()
  474. {
  475.     if (nerror) exitstatus = EXIT_FAILURE;
  476.     Izclose(&finptr);
  477.     ORCSclose();
  478. #    if !large_memory
  479.         if (fcopy!=workstdout) Ozclose(&fcopy);
  480. #    endif
  481.     if (neworkptr!=workstdout) Ozclose(&neworkptr);
  482.     dirtempunlink();
  483. }
  484.  
  485. #if RCS_lint
  486. #    define exiterr coExit
  487. #endif
  488.     exiting void
  489. exiterr()
  490. {
  491.     dirtempunlink();
  492.     tempunlink();
  493.     _exit(EXIT_FAILURE);
  494. }
  495.  
  496.  
  497. /*****************************************************************
  498.  * The following routines are auxiliary routines
  499.  *****************************************************************/
  500.  
  501.     static int
  502. rmworkfile()
  503. /*
  504.  * Prepare to remove workname, if it exists, and if
  505.  * it is read-only.
  506.  * Otherwise (file writable):
  507.  *   if !quietmode asks the user whether to really delete it (default: fail);
  508.  *   otherwise failure.
  509.  * Returns true if permission is gotten.
  510.  */
  511. {
  512.     if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
  513.         /* File is writable */
  514.         if (!yesorno(false, "writable %s exists%s; remove it? [ny](n): ",
  515.             workname,
  516.             myself(workstat.st_uid) ? "" : ", and you do not own it"
  517.         )) {
  518.         error(!quietflag && ttystdin()
  519.             ? "checkout aborted"
  520.             : "writable %s exists; checkout aborted", workname);
  521.         return false;
  522.             }
  523.         }
  524.     /* Actual unlink is done later by caller. */
  525.     return true;
  526. }
  527.  
  528.  
  529.     static int
  530. rmlock(delta)
  531.     struct hshentry const *delta;
  532. /* Function: removes the lock held by caller on delta.
  533.  * Returns -1 if someone else holds the lock,
  534.  * 0 if there is no lock on delta,
  535.  * and 1 if a lock was found and removed.
  536.  */
  537. {       register struct lock * next, * trail;
  538.     char const *num;
  539.         struct lock dummy;
  540.         int whomatch, nummatch;
  541.  
  542.         num=delta->num;
  543.         dummy.nextlock=next=Locks;
  544.         trail = &dummy;
  545.         while (next!=nil) {
  546.         whomatch = strcmp(getcaller(), next->login);
  547.                 nummatch=strcmp(num,next->delta->num);
  548.                 if ((whomatch==0) && (nummatch==0)) break;
  549.             /*found a lock on delta by caller*/
  550.                 if ((whomatch!=0)&&(nummatch==0)) {
  551.                     error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
  552.                     return -1;
  553.                 }
  554.                 trail=next;
  555.                 next=next->nextlock;
  556.         }
  557.         if (next!=nil) {
  558.                 /*found one; delete it */
  559.                 trail->nextlock=next->nextlock;
  560.                 Locks=dummy.nextlock;
  561.                 next->delta->lockedby=nil; /* reset locked-by */
  562.                 return 1; /*success*/
  563.         } else  return 0; /*no lock on delta*/
  564. }
  565.  
  566.  
  567.  
  568.  
  569. /*****************************************************************
  570.  * The rest of the routines are for handling joins
  571.  *****************************************************************/
  572.  
  573.  
  574.     static char const *
  575. addjoin(joinrev)
  576.     char *joinrev;
  577. /* Add joinrev's number to joinlist, yielding address of char past joinrev,
  578.  * or nil if no such revision exists.
  579.  */
  580. {
  581.     register char *j;
  582.     register struct hshentry const *d;
  583.     char terminator;
  584.     struct buf numrev;
  585.     struct hshentries *joindeltas;
  586.  
  587.     j = joinrev;
  588.     for (;;) {
  589.         switch (*j++) {
  590.         default:
  591.             continue;
  592.         case 0:
  593.         case ' ': case '\t': case '\n':
  594.         case ':': case ',': case ';':
  595.             break;
  596.         }
  597.         break;
  598.     }
  599.     terminator = *--j;
  600.     *j = 0;
  601.     bufautobegin(&numrev);
  602.     d = 0;
  603.     if (expandsym(joinrev, &numrev))
  604.         d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
  605.     bufautoend(&numrev);
  606.     *j = terminator;
  607.     if (d) {
  608.         joinlist[++lastjoin] = d->num;
  609.         return j;
  610.     }
  611.     return nil;
  612. }
  613.  
  614.     static int
  615. preparejoin()
  616. /* Function: Parses a join list pointed to by join and places pointers to the
  617.  * revision numbers into joinlist.
  618.  */
  619. {
  620.     register char const *j;
  621.  
  622.         j=join;
  623.         lastjoin= -1;
  624.         for (;;) {
  625.                 while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
  626.                 if (*j=='\0') break;
  627.                 if (lastjoin>=joinlength-2) {
  628.                         error("too many joins");
  629.                         return(false);
  630.                 }
  631.         if (!(j = addjoin(j))) return false;
  632.                 while ((*j==' ') || (*j=='\t')) j++;
  633.                 if (*j == ':') {
  634.                         j++;
  635.                         while((*j==' ') || (*j=='\t')) j++;
  636.                         if (*j!='\0') {
  637.                 if (!(j = addjoin(j))) return false;
  638.                         } else {
  639.                                 error("join pair incomplete");
  640.                                 return false;
  641.                         }
  642.                 } else {
  643.                         if (lastjoin==0) { /* first pair */
  644.                                 /* common ancestor missing */
  645.                                 joinlist[1]=joinlist[0];
  646.                                 lastjoin=1;
  647.                                 /*derive common ancestor*/
  648.                 if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
  649.                                        return false;
  650.                         } else {
  651.                                 error("join pair incomplete");
  652.                                 return false;
  653.                         }
  654.                 }
  655.         }
  656.         if (lastjoin<1) {
  657.                 error("empty join");
  658.                 return false;
  659.         } else  return true;
  660. }
  661.  
  662.  
  663.  
  664.     static char const *
  665. getancestor(r1, r2)
  666.     char const *r1, *r2;
  667. /* Yield the common ancestor of r1 and r2 if successful, nil otherwise.
  668.  * Work reliably only if r1 and r2 are not branch numbers.
  669.  */
  670. {
  671.     static struct buf t1, t2;
  672.  
  673.     int l1, l2, l3;
  674.     char const *r;
  675.  
  676.     l1 = countnumflds(r1);
  677.     l2 = countnumflds(r2);
  678.     if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
  679.         /* not on main trunk or identical */
  680.         l3 = 0;
  681.         while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
  682.         l3 += 2;
  683.         /* This will terminate since r1 and r2 are not the same; see above. */
  684.         if (l3==0) {
  685.         /* no common prefix; common ancestor on main trunk */
  686.         VOID partialno(&t1, r1, l1>2 ? 2 : l1);
  687.         VOID partialno(&t2, r2, l2>2 ? 2 : l2);
  688.         r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
  689.         if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
  690.             return r;
  691.         } else if (cmpnumfld(r1, r2, l3+1)!=0)
  692.             return partialno(&t1,r1,l3);
  693.     }
  694.     error("common ancestor of %s and %s undefined", r1, r2);
  695.     return nil;
  696. }
  697.  
  698.  
  699.  
  700.     static int
  701. buildjoin(initialfile)
  702.     char const *initialfile;
  703. /* Function: merge pairs of elements in joinlist into initialfile
  704.  * If workstdout is set, copy result to stdout.
  705.  * All unlinking of initialfile, rev2, and rev3 should be done by tempunlink().
  706.  */
  707. {
  708.     struct buf commarg;
  709.     struct buf subs;
  710.     char const *rev2, *rev3;
  711.         int i;
  712.     char const *cov[10], *mergev[12];
  713.     char const **p;
  714.  
  715.     bufautobegin(&commarg);
  716.     bufautobegin(&subs);
  717.     rev2 = maketemp(0);
  718.     rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */
  719.  
  720.     cov[0] = nil;
  721.     /* cov[1] setup below */
  722.     cov[2] = CO;
  723.     /* cov[3] setup below */
  724.     p = &cov[4];
  725.     if (expandarg) *p++ = expandarg;
  726.     if (suffixarg) *p++ = suffixarg;
  727.     if (versionarg) *p++ = versionarg;
  728.     *p++ = quietarg;
  729.     *p++ = RCSname;
  730.     *p = nil;
  731.  
  732.     mergev[0] = nil;
  733.     mergev[1] = nil;
  734.     mergev[2] = MERGE;
  735.     mergev[3] = mergev[5] = "-L";
  736.     /* rest of mergev setup below */
  737.  
  738.         i=0;
  739.         while (i<lastjoin) {
  740.                 /*prepare marker for merge*/
  741.                 if (i==0)
  742.             bufscpy(&subs, targetdelta->num);
  743.         else {
  744.             bufscat(&subs, ",");
  745.             bufscat(&subs, joinlist[i-2]);
  746.             bufscat(&subs, ":");
  747.             bufscat(&subs, joinlist[i-1]);
  748.         }
  749.         diagnose("revision %s\n",joinlist[i]);
  750.         bufscpy(&commarg, "-p");
  751.         bufscat(&commarg, joinlist[i]);
  752.         cov[1] = rev2;
  753.         cov[3] = commarg.string;
  754.         if (runv(cov))
  755.             goto badmerge;
  756.         diagnose("revision %s\n",joinlist[i+1]);
  757.         bufscpy(&commarg, "-p");
  758.         bufscat(&commarg, joinlist[i+1]);
  759.         cov[1] = rev3;
  760.         cov[3] = commarg.string;
  761.         if (runv(cov))
  762.             goto badmerge;
  763.         diagnose("merging...\n");
  764.         mergev[4] = subs.string;
  765.         mergev[6] = joinlist[i+1];
  766.         p = &mergev[7];
  767.         if (quietflag) *p++ = quietarg;
  768.         if (lastjoin<=i+2 && workstdout) *p++ = "-p";
  769.         *p++ = initialfile;
  770.         *p++ = rev2;
  771.         *p++ = rev3;
  772.         *p = nil;
  773.         switch (runv(mergev)) {
  774.             case DIFF_FAILURE: case DIFF_SUCCESS:
  775.             break;
  776.             default:
  777.             goto badmerge;
  778.         }
  779.                 i=i+2;
  780.         }
  781.     bufautoend(&commarg);
  782.     bufautoend(&subs);
  783.         return true;
  784.  
  785.     badmerge:
  786.     nerror++;
  787.     bufautoend(&commarg);
  788.     bufautoend(&subs);
  789.     return false;
  790. }
  791.